package com.practicesuite.controllayer;

import javax.swing.JPanel;

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;

import com.practicesuite.systemlayer.Doctor;
import com.practicesuite.systemlayer.SystemDriver;
import com.practicesuite.systemlayer.TreatmentRecord;
import com.practicesuite.systemlayer.User;

import java.awt.FlowLayout;
import javax.swing.SwingConstants;
import java.awt.BorderLayout;
import java.util.List;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Panel for viewing a list of all treatment records.
 * 
 * @author James
 *
 */
public class TreatmentListPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    public MainWindow window;
    
    public List<TreatmentRecord> treatments;
    public String[] visibleTreatments;
    public TreatmentRecord treatment;
    public int treatmentIndex;
    

    /**
     * Create the panel.
     */
    public TreatmentListPanel(MainWindow container, final User user) {
        this.window = container;
        setLayout(new GridLayout(0, 1, 0, 0));
        setPreferredSize(new Dimension(500, 400));
        
        try {
            treatments = SystemDriver.getAllTreatmentRecords();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "System error, please contact administrator.");
        }
        visibleTreatments = new String[treatments.size()];
        for (int i = 0; i < treatments.size(); i++) {
            TreatmentRecord curT = treatments.get(i);
            try {
                Doctor curD = SystemDriver.getDoctorById(curT.getTreatingDoctor());
                visibleTreatments[i] = curT.getDateTime().toString() + " (" + curD.getName() + ")" 
                    + "(Tret. Record ID" + curT.getTreatRecId() + ")";
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "System error, please contact administrator.");
            }
        }
        
        JPanel panel = new JPanel();
        add(panel);
        panel.setLayout(new BorderLayout(0, 0));
        
        JPanel panel_1 = new JPanel();
        panel.add(panel_1, BorderLayout.NORTH);
        panel_1.setLayout(new GridLayout(0, 1, 0, 0));
        panel_1.setPreferredSize(new Dimension(500, 100));
        
        JLabel lblPatientList = new JLabel("Treatment Record List");
        lblPatientList.setHorizontalAlignment(SwingConstants.CENTER);
        panel_1.add(lblPatientList);
        lblPatientList.setFont(new Font("Tahoma", Font.PLAIN, 24));
        
        JPanel panel_2 = new JPanel();
        panel_1.add(panel_2);
        FlowLayout fl_panel_2 = new FlowLayout(FlowLayout.CENTER, 5, 5);
        panel_2.setLayout(fl_panel_2);
        
        final JList treatmentList = new JList(visibleTreatments);
        treatmentList.setFont(new Font("Tahoma", Font.PLAIN, 12));
        treatmentList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        JScrollPane scrollPane = new JScrollPane(treatmentList);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        panel.add(scrollPane);
        
        JButton btnEditPatientInfo = new JButton("View Treatment Record Details");
        btnEditPatientInfo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                treatmentIndex = treatmentList.getSelectedIndex();
                try {
                    treatment = treatments.get(treatmentIndex);
                    window.gotoPanel("viewTreatment", treatment, user);
                } catch (IndexOutOfBoundsException ie) {
                    ie.printStackTrace();
                    JOptionPane.showMessageDialog(null, "Please select a treatment record.");
                } catch (Exception e) {
                    e.printStackTrace();
                    JOptionPane.showMessageDialog(null, "System error, please contact administrator.");
                }
            }
        });
        panel_2.add(btnEditPatientInfo);
        btnEditPatientInfo.setFont(new Font("Tahoma", Font.PLAIN, 14));
        
        JButton btnDeleteTreatmentRecord = new JButton("Delete Treatment Record");
        btnDeleteTreatmentRecord.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                treatmentIndex = treatmentList.getSelectedIndex();
                try {
                    treatment = treatments.get(treatmentIndex);
                    SystemDriver.deleteTreatmentRecord(treatment.getTreatRecId());
                    JOptionPane.showMessageDialog(null, "Treatment record deleted successfully.");
                    if (user.getEUID() == 0) {
                        window.gotoPanel("adminHome", user);
                    }
                    else if (user.getEUID() == 1) {
                        window.gotoPanel("doctorHome", user);
                    }
                    else if (user.getEUID() == 2) {
                        window.gotoPanel("nurseHome", user);
                    }
                } catch (IndexOutOfBoundsException ie) {
                    ie.printStackTrace();
                    JOptionPane.showMessageDialog(null, "Please select a treatment record.");
                } catch (Exception e) {
                    e.printStackTrace();
                    JOptionPane.showMessageDialog(null, "System error, please contact administrator.");
                }
            }
        });
        btnDeleteTreatmentRecord.setFont(new Font("Tahoma", Font.PLAIN, 14));
        panel_2.add(btnDeleteTreatmentRecord);
        
        JPanel panel_3 = new JPanel();
        panel.add(panel_3, BorderLayout.SOUTH);
        panel_3.setPreferredSize(new Dimension(500, 50));
        
        JButton btnReturnToHome = new JButton("Return to Home");
        btnReturnToHome.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (user.getEUID() == 0) {
                    window.gotoPanel("adminHome", user);
                }
                else if (user.getEUID() == 1) {
                    window.gotoPanel("doctorHome", user);
                }
                else if (user.getEUID() == 2) {
                    window.gotoPanel("nurseHome", user);
                }
            }
        });
        btnReturnToHome.setFont(new Font("Tahoma", Font.PLAIN, 14));
        panel_3.add(btnReturnToHome);

    }

}
